using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace MicroblazeConsoleSimulator
{
  class Output
  {
    private string name;

    private string filename;
    public string Filename
    {
      get { return filename; }
    }

    private Stream stream;
    private TextWriter textWriter = Console.Out;

    public TextWriter TextWriter
    {
      get { return textWriter; }
    }

    public bool RedirectToFile(string filename)
    {
      this.filename = filename;

      try
      {
        stream = File.Open(filename, FileMode.Create);
        textWriter = new StreamWriter(stream);
      }
      catch (Exception)
      {
        return false;
      }

      return true;
    }

    public void Close()
    {
      if (textWriter != null)
        textWriter.Close();

      if (stream != null)
        stream.Close();
    }

    public Output(string name)
    {
      this.name = name;
    }

    public string Target
    {
      get
      {
        return stream == null ? "stdout" : filename;
      }
    }
  }
}
